const util = require('../../utils/util')
const app = getApp();
Page({
  data:{
    api: app.globalData.api,
    products: [], //订单表不是商品表 count ctime hot id logo pid price product uid
    uid: '0',
    pid: 0,
    productinfo: {}
  },
  onLoad(options) {
    this.data.uid=wx.getStorageSync("uid")
    console.log(this.data.uid)
    util.http('/cart/list?uid='+this.data.uid, resp=>{
      this.data.products=resp
      console.log("购物车信息")
      console.log(resp)
      this.setData(this.data)
    })
  },
  onShow(){
    util.http('/cart/list?uid='+this.data.uid, resp=>{
      this.data.products=resp
      this.setData(this.data)
    })
  },
  onPullDownRefresh() { //下拉更新购物车列表
    util.http('/cart/list?uid='+this.data.uid, resp=>{
      this.data.products=resp
      this.setData(this.data)
      util.stopPullSetData(this)
    })
  },
  payHandler(e){
    var pid = e.currentTarget.dataset.pid
    util.http('/product/select?pid='+pid, resp=>{
      console.log("查询商品具体信息")
      console.log(resp);
      this.data.productinfo = resp;
      this.setData(this.data)

      wx.setStorageSync('商品信息', this.data.productinfo)
      console.log("购物车价格")
      console.log(this.data.productinfo.price)
      wx.setStorageSync('商品单价', this.data.productinfo.price)
      util.redirect('createorder')
    })

    
  },
  deleteHandler(e){
    var id = e.currentTarget.dataset.id
    console.log(id)
    util.http('/cart/del?id='+id, resp=>{
      console.log(resp)
      this.onLoad()
      this.setData(this.data)
      util.stopPullSetData(this)
    })
  }
})